package com.bainuo.doctor.common.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by admin on 2017/12/13.
 * stickerHeaderRecyclerView
 * 其中stickHeader需要与适配器的item的header保持一致的样式和高度
 */
public class StickHeaderRecyclerView extends RecyclerView {

    private View stickHeader;
    private int stickHeaderHeight;
    private LinearLayoutManager layoutManager;
    private int mCurrentPosition = 0;
    private OnScrollListener userScrollListner;
    private OnUpdateListener updateListener;

    public StickHeaderRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void setLayoutManager(LayoutManager layout) {
        layoutManager = new LinearLayoutManager(getContext());
        addOnScrollListener(scrollListener);
        super.setLayoutManager(layoutManager);
    }

    public void setStickHeader(View header) {
        this.stickHeader = header;
    }

    public void setUpdateListener(OnUpdateListener listener) {
        this.updateListener = listener;
    }

    @Override
    public void addOnScrollListener(OnScrollListener listener) {
        if (listener != scrollListener) {
            this.userScrollListner = listener;
        }
        super.addOnScrollListener(listener);
    }

    private OnScrollListener scrollListener = new OnScrollListener() {

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (stickHeader == null) {
                throw new NullPointerException("the stickHeader is null.");
            }
            stickHeaderHeight = stickHeader.getMeasuredHeight();
            if (userScrollListner != null) {
                userScrollListner.onScrollStateChanged(recyclerView, newState);
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (stickHeader == null) {
                throw new NullPointerException("the stickHeader is null.");
            }

            View view = layoutManager.findViewByPosition(mCurrentPosition + 1);

            if (view != null && view instanceof ViewGroup) {
                ViewGroup child = (ViewGroup) ((ViewGroup) view).getChildAt(0);
                if (child != null){
                    View header = child.getChildAt(0);
                    if (header.getVisibility() == View.VISIBLE && view.getTop() <= stickHeaderHeight) {
                        stickHeader.setY(-(stickHeaderHeight - view.getTop()));
                    } else {
                        stickHeader.setY(0);
                    }
                }
            }

            if (mCurrentPosition != layoutManager.findFirstVisibleItemPosition()) {
                mCurrentPosition = layoutManager.findFirstVisibleItemPosition();
                if (view == null)
                     stickHeader.setY(0);

                if (updateListener != null) {
                    updateListener.onUpdate(mCurrentPosition);
                }
            }
            if (userScrollListner != null) {
                userScrollListner.onScrolled(recyclerView, dx, dy);
            }
        }

    };

    public interface OnUpdateListener {
        void onUpdate(int itemVisiblePos);
    }

}
